/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * MenuCompraPeixes.java
 *
 * Created on 19/11/2009, 17:55:58
 */

package JPaneis;

import Conector.Conector;
import java.io.FileInputStream;
import java.io.IOException;
import java.sql.SQLException;
import java.util.Properties;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.ImageIcon;
import javax.swing.JFormattedTextField;
import javax.swing.JOptionPane;
import siii.MenuPadrao;


public class MenuCadastrarCompraPeixes extends javax.swing.JPanel {

    private MenuPadrao menuPadrao;
    private Conector conector;
    ImageIcon iconeOK;
    ImageIcon iconeERRO;

    /** Creates new form MenuCompraPeixes */
    public MenuCadastrarCompraPeixes(MenuPadrao menu, Conector con) {
        initComponents();
        getPeixesExpecies();
        this.menuPadrao = menu;
        this.conector = con;
        iconeOK = new ImageIcon(getClass().getResource("/Images/fish-48x48ok.png"));
        iconeERRO = new ImageIcon(getClass().getResource("/Images/fish-48x48erro.png"));
    }

        public void getPeixesExpecies(){
           Properties props = new Properties();

          //try retrieve data from file
             try {

             props.load(new FileInputStream(java.net.URLDecoder.decode(getClass().getResource("/Conector/configuracoes.properties").getPath())));

             String message = props.getProperty("especies");

             jTextFieldEspecie.setModel(new javax.swing.DefaultComboBoxModel(message.split(",")));
             }

             //catch exception in case properties file does not exist

             catch(IOException e)
             {
             e.printStackTrace();
             }
    }


    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabelEspecie = new javax.swing.JLabel();
        jLabelPeso = new javax.swing.JLabel();
        jLabelValor = new javax.swing.JLabel();
        jLabelDestino = new javax.swing.JLabel();
        jComboBoxDestino = new javax.swing.JComboBox();
        jLabelComprarPeixePiscina = new javax.swing.JLabel();
        jButton1 = new javax.swing.JButton();
        jButtonCancelar = new javax.swing.JButton();
        jButtonSalvar = new javax.swing.JButton();
        jLabelgramas = new javax.swing.JLabel();
        jTextFieldEspecie = new javax.swing.JComboBox();
        jLabel13 = new javax.swing.JLabel();
        jTextFieldPeso = new javax.swing.JFormattedTextField();
        jTextFieldValor = new javax.swing.JFormattedTextField();

        setPreferredSize(new java.awt.Dimension(1000, 500));

        jLabelEspecie.setText("Espécie");

        jLabelPeso.setText("Peso");

        jLabelValor.setText("Valor");

        jLabelDestino.setText("Destino");

        jComboBoxDestino.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Piscina", "Tanque" }));

        jLabelComprarPeixePiscina.setFont(new java.awt.Font("Tahoma", 0, 24));
        jLabelComprarPeixePiscina.setText("Cadastrar Compra de Peixes ");

        jButton1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Images/peixe_limpar.png"))); // NOI18N
        jButton1.setText("Limpar");
        jButton1.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jButton1MouseClicked(evt);
            }
        });

        jButtonCancelar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Images/peixe_cancelar.png"))); // NOI18N
        jButtonCancelar.setText("Cancelar");
        jButtonCancelar.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jButtonCancelarMouseClicked(evt);
            }
        });

        jButtonSalvar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Images/peixe_add.png"))); // NOI18N
        jButtonSalvar.setText("Salvar");
        jButtonSalvar.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jButtonSalvarMouseClicked(evt);
            }
        });

        jLabelgramas.setText("g");

        jTextFieldEspecie.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" }));

        jLabel13.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Images/compra_peixe_1.png"))); // NOI18N

        jTextFieldPeso.setFormatterFactory(new javax.swing.text.DefaultFormatterFactory(new javax.swing.text.NumberFormatter(new java.text.DecimalFormat("#,##0.###"))));
        jTextFieldPeso.setFocusLostBehavior(javax.swing.JFormattedTextField.PERSIST);

        jTextFieldValor.setFormatterFactory(new javax.swing.text.DefaultFormatterFactory(new javax.swing.text.NumberFormatter(new java.text.DecimalFormat("#,##0.##"))));
        jTextFieldValor.setFocusLostBehavior(javax.swing.JFormattedTextField.PERSIST);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                        .addGap(30, 30, 30)
                        .addComponent(jLabel13, javax.swing.GroupLayout.PREFERRED_SIZE, 136, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jLabelComprarPeixePiscina, javax.swing.GroupLayout.PREFERRED_SIZE, 562, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(layout.createSequentialGroup()
                        .addContainerGap(514, Short.MAX_VALUE)
                        .addComponent(jButtonSalvar, javax.swing.GroupLayout.PREFERRED_SIZE, 107, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jButtonCancelar)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 113, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addGap(143, 143, 143))
            .addGroup(layout.createSequentialGroup()
                .addGap(57, 57, 57)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jLabelDestino)
                        .addGap(31, 31, 31)
                        .addComponent(jComboBoxDestino, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addComponent(jLabelEspecie)
                        .addGap(57, 57, 57)
                        .addComponent(jTextFieldEspecie, javax.swing.GroupLayout.PREFERRED_SIZE, 165, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(84, 84, 84)
                        .addComponent(jLabelPeso)
                        .addGap(55, 55, 55)
                        .addComponent(jTextFieldPeso, javax.swing.GroupLayout.PREFERRED_SIZE, 102, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jLabelgramas)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 80, Short.MAX_VALUE)
                        .addComponent(jLabelValor)
                        .addGap(18, 18, 18)
                        .addComponent(jTextFieldValor, javax.swing.GroupLayout.PREFERRED_SIZE, 102, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(35, 35, 35)))
                .addGap(152, 152, 152))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(jLabel13))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(34, 34, 34)
                        .addComponent(jLabelComprarPeixePiscina, javax.swing.GroupLayout.DEFAULT_SIZE, 105, Short.MAX_VALUE)))
                .addGap(55, 55, 55)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabelDestino)
                    .addComponent(jComboBoxDestino, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(73, 73, 73)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabelEspecie)
                    .addComponent(jLabelPeso)
                    .addComponent(jLabelgramas)
                    .addComponent(jLabelValor)
                    .addComponent(jTextFieldEspecie, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jTextFieldPeso, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jTextFieldValor, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(98, 98, 98)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jButtonCancelar)
                    .addComponent(jButtonSalvar)
                    .addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 39, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(54, 54, 54))
        );
    }// </editor-fold>//GEN-END:initComponents

    private void jButton1MouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jButton1MouseClicked

        this.cleanScreen();
        // TODO add your handling code here:
    }//GEN-LAST:event_jButton1MouseClicked

    private void jButtonCancelarMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jButtonCancelarMouseClicked

        this.cleanScreen();
        menuPadrao.trocaPanel2(menuPadrao.getMenuPrincipal());
        // TODO add your handling code here:
    }//GEN-LAST:event_jButtonCancelarMouseClicked

    private void jButtonSalvarMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jButtonSalvarMouseClicked
        String peso;
        String valor;
        if(jTextFieldPeso.getText().contains(",") )
                peso = jTextFieldPeso.getText().replace(".","").replace(",", ".");
        else
            peso = jTextFieldPeso.getText();


        if(jTextFieldValor.getText().contains(","))
                valor = jTextFieldValor.getText().replace(".","").replace(",", ".");
        else
            valor = jTextFieldValor.getText();

//        if(verificaPesoValor(!jTextFieldPeso)){
//         JOptionPane.showMessageDialog(this, "PESO inválido", "Atenção", JOptionPane.WARNING_MESSAGE, iconeERRO);
//        }
//
//        if(verificaPesoValor(!jTextFieldValor)){
//         JOptionPane.showMessageDialog(this, "Valor inválido", "Atenção", JOptionPane.WARNING_MESSAGE, iconeERRO);
//        }

        if(jTextFieldPeso.getText().length() > 0 && jTextFieldValor.getText().length() > 0) {
                try {
                    conector.cadastraCompraPeixes(jComboBoxDestino.getSelectedItem().toString(), (String)jTextFieldEspecie.getSelectedItem(), Float.parseFloat(peso), Float.parseFloat(valor));
                    JOptionPane.showMessageDialog(this, "Compra armazenada com sucesso", "Atenção", JOptionPane.WARNING_MESSAGE, iconeOK);
                } catch (SQLException ex) {
                    Logger.getLogger(MenuCadastrarCompraPeixes.class.getName()).log(Level.SEVERE, null, ex);
                    conector = new Conector();
                }
            
        } else
            JOptionPane.showMessageDialog(this, "Dados inválidos", "Atenção", JOptionPane.WARNING_MESSAGE, iconeERRO);
        // TODO add your handling code here:
        this.cleanScreen();

    }//GEN-LAST:event_jButtonSalvarMouseClicked
        public boolean verificaPesoValor(JFormattedTextField verificar) {

        String valor;
        int i;
        boolean validade = true;
        char[] verif = new char[verificar.toString().replace(".", "").replace(",", "").replace(" ", "").length()];
        verif = verificar.getText().replace(".", "").replace(",", "").replace(" ", "").toCharArray();

        for(i = 0; i < verificar.toString().replace(".", "").replace(",", "").replace(" ", "").length(); i++)
             if(!Character.isDigit(verif[i]))
                      validade = false;
     return validade;
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButtonCancelar;
    private javax.swing.JButton jButtonSalvar;
    private javax.swing.JComboBox jComboBoxDestino;
    private javax.swing.JLabel jLabel13;
    private javax.swing.JLabel jLabelComprarPeixePiscina;
    private javax.swing.JLabel jLabelDestino;
    private javax.swing.JLabel jLabelEspecie;
    private javax.swing.JLabel jLabelPeso;
    private javax.swing.JLabel jLabelValor;
    private javax.swing.JLabel jLabelgramas;
    private javax.swing.JComboBox jTextFieldEspecie;
    private javax.swing.JFormattedTextField jTextFieldPeso;
    private javax.swing.JFormattedTextField jTextFieldValor;
    // End of variables declaration//GEN-END:variables

    public void cleanScreen() {

        jComboBoxDestino.setSelectedItem("Piscina");
        jTextFieldEspecie.setSelectedIndex(0);
        jTextFieldPeso.setText("");
        jTextFieldValor.setText("");

    }


}
